package myJs

import com.karasiq.bootstrap.Bootstrap.default._
import myJs.Utils._
import myJs.myPkg._
import org.singlespaced.d3js.Ops._
import org.singlespaced.d3js.d3
import shared.Implicits._

import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import myJs.Implicits._
import myJs.myPkg.jquery._
import org.scalajs.dom.document

/**
  * Created by yz on 2019/4/15
  */
@JSExportTopLevel("Predict")
object Predict {

  @JSExport("init")
  def init = {
    bootStrapValidator
    registerEvent
//    showResult("322")


  }

  def registerEvent = {
    $(document.body).on("click", s".myReduceNum", "", (y: Element) => {
      val textJq = $(y).parent().next()
      val value = textJq.`val`().toString.toInt
      textJq.`val`(s"${value - 1}")
      val bv = $("#form").bv
      bv.revalidateField(textJq)
    })
    $(document.body).on("click", s".myAddNum", "", (y: Element) => {
      val textJq = $(y).parent().prev()
      val value = textJq.`val`().toString.toInt
      textJq.`val`(s"${value + 1}")
      val bv = $("#form").bv
      bv.revalidateField(textJq)
    })
  }

  @JSExport("myRun")
  def myRun = {
    val bv = jQuery("#form").data("bootstrapValidator")
    bv.validate()
    val valid = bv.isValid().asInstanceOf[Boolean]
    if (valid) {
      val data = $("#form").serialize()
      val url = g.jsRoutes.controllers.PredictController.predict().url.toString
      val index = layer.alert(element, layerOptions)
      val ajaxSettings = JQueryAjaxSettings.url(url).
        `type`("post").data(data).success { (data, status, e) =>
        layer.close(index)
        val rs = data.asInstanceOf[js.Dictionary[js.Any]]
        if (rs.myGet("valid") == "false") {
          Swal.swal(SwalOptions.title("错误").text(rs.myGet("message")).`type`("error"))
          $("#result").hide()
        } else {
          val missionId=rs.myGet("missionId")
          PredictResult.showResult(missionId)

        }
      }
      $.ajax(ajaxSettings)
    }

  }

  def bootStrapValidator = {
    val maxNumber = Double.MaxValue
    val dict: js.Dictionary[js.Any] = js.Dictionary(
      "feedbackIcons" -> js.Dictionary(
        "valid" -> "glyphicon glyphicon-ok",
        "invalid" -> "",
        "validating" -> "glyphicon glyphicon-refresh",
      ),
      "fields" -> js.Dictionary(
        "sampleId" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "样本号不能为空！"
            ),
          )
        ),
        "age" -> {
          val info = "年龄"
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> s"${info}不能为空！"
              ),
              "numeric" -> js.Dictionary(
                "message" -> s"${info}必须为数字!",
              ),
              "between" -> js.Dictionary(
                "min" -> 18,
                "max" -> 100,
                "message" -> s"${info}必须处于18到100之间！",
              ),
            )
          )
        },
        "leu" -> {
          val info = "L-亮氨酸（Leu）"
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> s"${info}不能为空！"
              ),
              "numeric" -> js.Dictionary(
                "message" -> s"${info}必须为数字!",
              ),
            )
          )
        },
        "ile" -> {
          val info = "L-异亮氨酸（Ile）"
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> s"${info}不能为空！"
              ),
              "numeric" -> js.Dictionary(
                "message" -> s"${info}必须为数字!",
              ),
            )
          )
        },
        "val" -> {
          val info = "L-缬氨酸（Val）"
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> s"${info}不能为空！"
              ),
              "numeric" -> js.Dictionary(
                "message" -> s"${info}必须为数字!",
              ),
            )
          )
        },
        "phe" -> {
          val info = "L-苯丙氨酸（Phe）"
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> s"${info}不能为空！"
              ),
              "numeric" -> js.Dictionary(
                "message" -> s"${info}必须为数字!",
              ),
            )
          )
        },
        "tyr" -> {
          val info = "L-酪氨酸（Tyr）"
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> s"${info}不能为空！"
              ),
              "numeric" -> js.Dictionary(
                "message" -> s"${info}必须为数字!",
              ),
            )
          )
        },
      )
    )
    $(s"#form").bootstrapValidator(dict)

  }


}
